package com.button.job;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.util.ShardingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TestJobSimple {

    @XxlJob(value = "simpleJobHandler")
    public ReturnT<String> simpleJobHandler(String arg) {
        log.info("一个简单的job. arg={}", arg);
        return ReturnT.SUCCESS;
    }

    @XxlJob(value = "shardingJobHandler")
    public ReturnT<String> shardingJobHandler(String param) throws Exception{
        log.info("shardingJobHandler -> param={}", param);
        // 分片参数
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        log.info("总分片数 -> total={}", shardingVO.getTotal());
        // 业务逻辑
        for (int i = 0; i < shardingVO.getTotal(); i++) {
            if (i == shardingVO.getIndex()) {
                log.info("第 {} 片, 命中分片开始处理", i);
            } else {
                log.info("第 {} 片, 忽略", i);
            }
        }
        return ReturnT.SUCCESS;
    }
}
